import BaseUtil from '@/BaseUtil';
import MathUtil from '@/MathUtil';
import RandomUtil from '@/RandomUtil';
import SelectUtil from '@/SelectUtil';
import { OrderId } from '@/globals/order';
import MovementCommon from 'code/Core/Common/MovementCommon';
import VestCommon from 'code/Core/Common/VestCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 蛛网聚
 **/
export default class BossSpell_zhuwang extends AbilityBase {
    static instance: BossSpell_zhuwang;
    constructor(id: string = 'Mzw8') {
        super(id);
        BossSpell_zhuwang.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {}
    damagePool: unit[] = [];
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        BossAbilityManagerInstance.text('蛛网聚', source);
        let x = GetUnitX(source);
        let y = GetUnitY(source);
        let ms: unit[] = [];
        this.damagePool = [];
        let f = RandomUtil.randomAngle();
        for (let i = 0; i < 8; i++) {
            let { x: tx, y: ty } = MathUtil.polarProjection(x, y, 700, f);
            let ang = MathUtil.angleBetweenCoords(tx, ty, x, y);
            let v = VestCommon.取马甲并设置(tx, ty, ang);
            DzSetUnitModel(v, 'Abilities\\Spells\\Undead\\OrbOfDeath\\OrbOfDeathMissile.mdl');
            ms.push(v);
            f += 45;
        }
        BaseUtil.runLater(1.5, () => {
            for (let u of ms) {
                MovementCommon.hitFly({
                    target: u,
                    caster: source,
                    angle: GetUnitFacing(u),
                    speed: 1000,
                    distance: 700,
                    maxHeight: 0,
                    onInterval: this.moveInterval.bind(this),
                    onEnd: this.onMoveEnd.bind(this),
                });
            }
        });
    }
    moveInterval(p: HitFlyConfig) {
        let es = SelectUtil.getEnemyUnitsInRange(p.caster, 75, p.curX, p.curY);
        for (let e of es) {
            SetUnitX(e, p.curX);
            SetUnitY(e, p.curY);
            if (!this.damagePool.includes(e)) {
                this.damagePool.push(e);
                DamageSystemInstance.applyDarkDamage(e, p.caster, 1000);
            }
        }
    }
    onMoveEnd(p: HitFlyConfig) {
        VestCommon.returnVest(p.target);
        IssueImmediateOrderById(p.caster, OrderId.Stomp);
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
}
